package cn.com.dashihui.web.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import cn.com.dashihui.web.dao.BestSelling;
import cn.com.dashihui.web.dao.BestSellingTitle;
import cn.com.dashihui.web.dao.GoodsStore;

public class BestSellingService {
	public boolean sortAd(Map<String,String> sortMap){
		int batchSize = sortMap.size();
		List<String> sqlList = new ArrayList<String>();
		for(String id : sortMap.keySet()){
			sqlList.add("UPDATE t_bus_goods_base_bestselling SET orderNo="+sortMap.get(id)+" WHERE id="+id);
		}
		int[] result = Db.batch(sqlList,batchSize);
		return result.length>0;
	}
	
	public List<BestSelling> findAll(){
		return BestSelling.me().find("SELECT sa.*,sg.name goodsName FROM t_bus_goods_base_bestselling sa LEFT JOIN t_bus_goods sg ON sa.goodsid=sg.id ORDER BY sa.orderNo");
	}
	
	public boolean addAd(BestSelling newObject){
		return newObject.save();
	}
	
	public boolean delAd(int id){
		return BestSelling.me().deleteById(id);
	}
	
	public boolean editAd(BestSelling object){
		return object.update();
	}
	
	public BestSelling findById(int id){
		return BestSelling.me().findFirst("SELECT sa.*,sg.name goodsName FROM t_bus_goods_base_bestselling sa LEFT JOIN t_bus_goods sg ON sa.goodsid=sg.id WHERE sa.id=?",id);
	}

	public Page<GoodsStore> getStorePage(int sGoodId,int sCategoryonid, int sCategorytwid, String sSellerId, String sStoreId, String keyword,int pageNum, int pageSize) {
		StringBuffer sqlExcept = new StringBuffer("FROM t_bus_goods AS A INNER JOIN t_bus_goods_store_rel F ON A.good_store = F.goodstore inner JOIN t_dict_store AS C ON F.storeid= C.storeid inner JOIN t_bus_seller AS B ON B.sellerId= C.sellerId "
				+ " LEFT JOIN t_dict_category D on A.categoryonid=D.categoryId LEFT JOIN t_dict_category E on A.categorytwid=E.categoryId where A.pstate !=3 and A.state=2 and C.state=2 and qstate !=3 ");
		List<Object> params = new ArrayList<Object>();
		if(sGoodId!=0){
			sqlExcept.append(" AND A.id=?");
			params.add(sGoodId);
		}
		if(!StrKit.isBlank(sSellerId)&& !"0".equals(sSellerId)){
			sqlExcept.append(" AND B.sellerId= ?");
			params.add(sSellerId);
		}
		if(!StrKit.isBlank(sStoreId)&&!"0".equals(sStoreId)){
			sqlExcept.append(" AND C.storeId= ?");
			params.add(sStoreId);
		}
		 if(sCategorytwid!=0){
			 sqlExcept.append(" AND A.categoryonid=? AND A.categorytwid=?");
			params.add(sCategoryonid);params.add(sCategorytwid);
		}else if(sCategoryonid!=0){
			sqlExcept.append(" AND A.categoryonid=?");
			params.add(sCategoryonid);
		}
		 if(!StrKit.isBlank(keyword)){
			sqlExcept.append(" AND A.name LIKE ?");
			params.add("%"+keyword+"%");
		}
		 sqlExcept.append(" group by A.id");
		return GoodsStore.me().paginate(pageNum, pageSize, "SELECT  A.* ", sqlExcept.toString(), params.toArray());
	}

	public BestSellingTitle findBestSellingTitle() {
		return BestSellingTitle.me().findFirst("select * from t_bus_goods_base_bestselling_title");
	}

	public BestSellingTitle findBestSellingTitleById(int id) {
		return BestSellingTitle.me().findFirst("select * from t_bus_goods_base_bestselling_title where id=?",id);
	}

}
